using System.Windows.Forms;
using System.Drawing;
using Drawing3d;
namespace Sample
{
    public partial class Form1 : Form
    {
        MyDevice Device = new MyDevice();
      
        public Form1()
        {
            InitializeComponent();
            Device.WinControl = this;
        }
        public void SetPanning(xy Delta)
        {
         Device.ProjectionMatrix=   Matrix.Translation((float)Delta.x * 2f / (float)Device.ViewPort.Width, (float)Delta.y * 2f / (float)Device.ViewPort.Height, 0) * Device.ProjectionMatrix;
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Left:
                    xyz B = Device.Camera.Angles;
                    Device.Camera.Angles = new xyz(0.2, 0.3,0.4);
                    Device.Camera.Angles = B;
                    Device.Camera.Yaw = Device.Camera.Yaw - 0.1;
                    break;
                case Keys.Right:
                    Device.Camera.Yaw = Device.Camera.Yaw + 0.1;
                    break;
                case Keys.Up:
                    Device.Camera.Pitch = Device.Camera.Pitch + 0.1;
                    break;
                case Keys.Down:
                    Device.Camera.Pitch = Device.Camera.Pitch - 0.1;
                     break;
                case Keys.R: // Roll
                    if (Form.ModifierKeys == Keys.Shift)
                        Device.Camera.Roll = Device.Camera.Roll - 0.1;
                    else
                        Device.Camera.Roll = Device.Camera.Roll + 0.1;
                     break;
                case Keys.W:// Walk in Z-Axis 0.5
                    if (Form.ModifierKeys == Keys.Shift)
                        Device.Camera.GoForward(-0.5f);
                    else
                        Device.Camera.GoForward(0.5f);
              
                    break;
                case Keys.X: // X-Panning
                    {
                        
                        Point Panning = Device.Camera.Panning;
                        if (Form.ModifierKeys == Keys.Shift)
                            Device.Camera.Panning = new Point(Panning.X - 5, Panning.Y);
                        else
                            Device.Camera.Panning = new Point(Panning.X + 5, Panning.Y);
                      
                    }
                    break;
                case Keys.Y: // Y-Panning
                    {
                        Point Panning = Device.Camera.Panning;
                        if (Form.ModifierKeys == Keys.Shift)
                            Device.Camera.Panning = new Point(Panning.X, Panning.Y - 5);
                        else
                            Device.Camera.Panning = new Point(Panning.X, Panning.Y + 5);
                      
                    }
                    break;
                case Keys.N:// Walk in XY- Plane of camera in x-direction
                    if (Form.ModifierKeys == Keys.Shift)
                        Device.Camera.GoForward(-0.2f);
                    else
                        Device.Camera.GoForward(0.2f);
                  
                    break;
                case Keys.M:// Walk in XY- Plane of camera in y-direction
                    if (Form.ModifierKeys == Keys.Shift)
                        Device.Camera.GoRight( -0.2f);
                    else
                        Device.Camera.GoRight(0.2f);
                  
                    break;
                case Keys.Z: // Zooming
                    if (Form.ModifierKeys == Keys.Shift)
                        Device.Camera.ZoomFactor = 0.9 * Device.Camera.ZoomFactor;
                    else
                        Device.Camera.ZoomFactor =1.1* Device.Camera.ZoomFactor;
                   
                    break;
            }
        }
    }
    public class MyDevice:OpenGlDevice
    {
    
        public override void OnPaint()
        {
           base.OnPaint();
            // Draw a lightgray grid
          
           Emission = Color.LightGray;
            int Count = 100;
            double Distance = 2;
            for (int j = 0; j < Count; j++)
                drawLine(new xyz(-Count / 2 * Distance, j * Distance - Count / 2 * Distance, 0), new xyz(Count / 2 * Distance, j * Distance - Count / 2 * Distance, 0));
            for (int j = 0; j < Count; j++)
                drawLine(new xyz(j * Distance - Count / 2 * Distance, -Count / 2 * Distance, 0), new xyz(j * Distance - Count / 2 * Distance, Count / 2 * Distance, 0));
            Emission = Color.Black;
            // save the ambient material
            Color SaveAmbient = Ambient;
            // and draw four Boxes
            Ambient = Color.Red;
            drawBox(new xyz(-4, -1, 0), new xyz(4, 1, 2));
            Ambient = Color.Green;
            drawBox(new xyz(-2, 8, 0), new xyz(4, 2, 5));
            Ambient = Color.Yellow;
            drawBox(new xyz(7, 5, 0), new xyz(3, 4, 4));
            Translucent = 0.6;
            Ambient = Color.Blue;
            drawBox( new xyz(1, 1, 0), new xyz(4, 3, 6));
            Translucent = 1;
            Ambient = SaveAmbient;
          
        }
       
        protected override void OnCreated()
        {
           
            base.OnCreated();
            BackColor = Color.DarkGray;
            Lights[0].Position = new xyz(-10, -5, 3);
            // Setzen des Blickwinkels : Perspektive
            FieldOfView = 30;
         
          
            SnapEnable = false;
            Camera.LookAt(new xyz(-20, -10, 2), new xyz(0, 0, 2), new xyz(0, 0, 1));
            Camera.SetRelativeSystem();
       
        }
       
    }
    
}